﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CPPEI.Coolzon.Designer.Views
{
    public static class PropertyUtility
    {
        public static void SetProperty(Coolzon.Designtime.Metadata.Solution solutionMetadata,
            Coolzon.Designtime.Metadata.Scene sceneMetadata,
            string partName,
            Coolzon.UI.Part partInstance,
            string propertyName,
            object propertyValue)
        {
            if (partInstance is CPPEI.Coolzon.UI.ScenePart)
            {
                SetSceneProperty(solutionMetadata, partName, partInstance as CPPEI.Coolzon.UI.ScenePart,
                    propertyName,
                    propertyValue);
            }
            else if (partInstance is CPPEI.Coolzon.UI.AreaPart)
            {
                SetAreaProperty(sceneMetadata, partName, partInstance as CPPEI.Coolzon.UI.AreaPart,
                    propertyName,
                    propertyValue);
            }
            else
            {
                SetPartProperty(sceneMetadata, partName,
                    partInstance,
                    propertyName,
                    propertyValue); 
            }
        }

        /// <summary>
        /// 设置场景的属性
        /// </summary>
        /// <param name="solutionMetadata"></param>
        /// <param name="sceneName"></param>
        /// <param name="sceneInstance"></param>
        /// <param name="propertyName"></param>
        /// <param name="propertyValue"></param>
        public static void SetSceneProperty(Coolzon.Designtime.Metadata.Solution solutionMetadata,
            string sceneName,
            Coolzon.UI.ScenePart sceneInstance,
            string propertyName,
            object propertyValue)
        {
            Coolzon.Designtime.Metadata.Scene sceneMetadata = Designtime.Metadata.MetadataUtil.GetMetadataNode(solutionMetadata, sceneName) as Designtime.Metadata.Scene;

            Coolzon.UI.XProperty.XPropertyDescription xPropertyDescription = Coolzon.UI.XPropertyUtility.GetXPropertyDescription(sceneInstance, propertyName);

            // 获取转换器
            Coolzon.UI.XProperty.IPropertyValueConverter convertor = Coolzon.UI.XProperty.PropertyValueConverterFactory.GetConverter(xPropertyDescription.PropertyInfo.PropertyType);
            xPropertyDescription.PropertyInfo.SetValue(sceneInstance, propertyValue);

            Coolzon.Metadata.Property targetPropertyMetadata = null;
            for (int index = 0; index < sceneMetadata.RT_Scene.Properties.Count; index++)
            {
                Coolzon.Metadata.Property propertyMetadata = sceneMetadata.RT_Scene.Properties[index];
                if (string.Equals(propertyMetadata.Name, propertyName, StringComparison.CurrentCultureIgnoreCase))
                {
                    targetPropertyMetadata = propertyMetadata;
                    break;
                }
            }

            if (targetPropertyMetadata == null)
            {
                targetPropertyMetadata = new Coolzon.Metadata.Property();
                targetPropertyMetadata.Name = propertyName;
                sceneMetadata.RT_Scene.Properties.Add(targetPropertyMetadata);
            }
            targetPropertyMetadata.ValueNode = convertor.ConvertBack(null, propertyValue);
        }

        /// <summary>
        /// 设置区域的属性
        /// </summary>
        /// <param name="sceneMetadata"></param>
        /// <param name="areaName"></param>
        /// <param name="areaInstance"></param>
        /// <param name="propertyName"></param>
        /// <param name="propertyValue"></param>
        public static void SetAreaProperty(Coolzon.Designtime.Metadata.Scene sceneMetadata,
            string areaName,
            Coolzon.UI.AreaPart areaInstance,
            string propertyName,
            object propertyValue)
        {
            Coolzon.Designtime.Metadata.Area areaMetadata = Designtime.Metadata.MetadataUtil.GetMetadataNode(sceneMetadata, areaName) as Designtime.Metadata.Area;

            Coolzon.UI.XProperty.XPropertyDescription xPropertyDescription = Coolzon.UI.XPropertyUtility.GetXPropertyDescription(areaInstance, propertyName);

            // 获取转换器
            Coolzon.UI.XProperty.IPropertyValueConverter convertor = Coolzon.UI.XProperty.PropertyValueConverterFactory.GetConverter(xPropertyDescription.PropertyInfo.PropertyType);
            xPropertyDescription.PropertyInfo.SetValue(areaInstance, propertyValue);

            Coolzon.Metadata.Property targetPropertyMetadata = null;
            for (int index = 0; index < areaMetadata.RT_Area.Properties.Count; index++)
            {
                Coolzon.Metadata.Property propertyMetadata = areaMetadata.RT_Area.Properties[index];
                if (string.Equals(propertyMetadata.Name, propertyName, StringComparison.CurrentCultureIgnoreCase))
                {
                    targetPropertyMetadata = propertyMetadata;
                    break;
                }
            }

            if (targetPropertyMetadata == null)
            {
                targetPropertyMetadata = new Coolzon.Metadata.Property();
                targetPropertyMetadata.Name = propertyName;
                areaMetadata.RT_Area.Properties.Add(targetPropertyMetadata);
            }
            targetPropertyMetadata.ValueNode = convertor.ConvertBack(null, propertyValue);
        }

        /// <summary>
        /// 设置组件的属性值
        /// </summary>
        /// <param name="sceneMetadata"></param>
        /// <param name="partName"></param>
        /// <param name="partInstance"></param>
        /// <param name="propertyName"></param>
        /// <param name="propertyValue"></param>
        public static void SetPartProperty(Coolzon.Designtime.Metadata.Scene sceneMetadata,
            string partName,
            Coolzon.UI.Part partInstance,
            string propertyName, 
            object propertyValue)
        {
            Coolzon.Designtime.Metadata.Part partMetadata = Designtime.Metadata.MetadataUtil.GetMetadataNode(sceneMetadata, partName) as Designtime.Metadata.Part;

            Coolzon.UI.XProperty.XPropertyDescription xPropertyDescription = Coolzon.UI.XPropertyUtility.GetXPropertyDescription(partInstance, propertyName);

            // 获取转换器
            Coolzon.UI.XProperty.IPropertyValueConverter convertor = Coolzon.UI.XProperty.PropertyValueConverterFactory.GetConverter(xPropertyDescription.PropertyInfo.PropertyType);
            xPropertyDescription.PropertyInfo.SetValue(partInstance, propertyValue);

            Coolzon.Metadata.Property targetPropertyMetadata = null;
            for (int index = 0; index < partMetadata.RT_Part.Properties.Count; index++)
            {
                Coolzon.Metadata.Property propertyMetadata = partMetadata.RT_Part.Properties[index];
                if (string.Equals(propertyMetadata.Name, propertyName, StringComparison.CurrentCultureIgnoreCase))
                {
                    targetPropertyMetadata = propertyMetadata;
                    break;
                }
            }

            if (targetPropertyMetadata == null)
            {
                targetPropertyMetadata = new Coolzon.Metadata.Property();
                targetPropertyMetadata.Name = propertyName;
                partMetadata.RT_Part.Properties.Add(targetPropertyMetadata);
            }
            targetPropertyMetadata.ValueNode = convertor.ConvertBack(null, propertyValue);
        }
    }
}
